//
//  LGVersionUpdateView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/17.
//  版本更新

#import "LGVersionUpdateView.h"

@interface LGVersionUpdateView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *bgImageView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *versionLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIButton *uptateBtn;
/**   */
@property (nonatomic , strong)UIButton *closeBtn;

@end

@implementation LGVersionUpdateView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [self showViewAnimation];
    }
    return self;
}

-(void)setNewerVersion:(NSString *)newerVersion{
    _newerVersion = newerVersion;
    self.versionLabel.text = [NSString stringWithFormat:@"v%@",newerVersion];
}

-(void)showViewAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 10000, 10000);
    }];
}

-(void)uptateBtnAction{
    if(self.updateVersionAction){
        self.updateVersionAction();
    }
    [self closeBtnAction];
}

-(void)closeBtnAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.0001, 0.0001);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.bgImageView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.versionLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.uptateBtn];
//    [self.baseView addSubview:self.closeBtn];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(27), (Screen_H-viewPix(302))/2.0, Screen_W-viewPix(54), viewPix(302))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.layer.cornerRadius = 8;
        _baseView.clipsToBounds = NO;
        _baseView.transform = CGAffineTransformScale(_baseView.transform, 0.0001, 0.0001);
    }
    return _baseView;
}

-(UIImageView *)bgImageView{
    if(!_bgImageView){
        _bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"版本更新-背景"]];
        _bgImageView.frame = CGRectMake(0, -viewPix(43), Screen_W-viewPix(54), viewPix(179));
    }
    return _bgImageView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"发现新版本" colorString:@"#FFFFFF" textFont:LGFontWeight(17, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
        _titleLabel.frame = CGRectMake(viewPix(24), viewPix(20), viewPix(150), viewPix(24));
    }
    return _titleLabel;
}

-(UILabel *)versionLabel{
    if(!_versionLabel){
        _versionLabel = [UILabel lableWithText:@"v" colorString:@"#FFFFFF" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
        _versionLabel.frame = CGRectMake(viewPix(24), viewPix(48), viewPix(100), viewPix(20));
    }
    return _versionLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:[NSString stringWithFormat:@"%@又有新版本了，升级到最新版本，享受更丰富、稳定、快速的功能和体验！",AppTitle] colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
        _contentLabel.frame = CGRectMake(viewPix(24), viewPix(130), Screen_W-viewPix(54)-viewPix(48), viewPix(70));
        [_contentLabel lineSpacing:4];
    }
    return _contentLabel;
}

-(UIButton *)uptateBtn{
    if(!_uptateBtn){
        _uptateBtn = [UIButton buttonWithTitle:@"立即升级" titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _uptateBtn.frame = CGRectMake(viewPix(90), viewPix(233), viewPix(140), viewPix(46));
        _uptateBtn.backgroundColor = [UIColor colorWithString:@"#3291FF"];
        _uptateBtn.cornerRidus = viewPix(23);
        [_uptateBtn addTarget:self action:@selector(uptateBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _uptateBtn;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithImage:@"版本更新-关闭" selectImage:@"版本更新-关闭"];
        _closeBtn.frame = CGRectMake(Screen_W-viewPix(54)-viewPix(36), 0, viewPix(36), viewPix(36));
        [_closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}


@end
